package com.recharge.srevice;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.ChildMeter;
import com.entity.Customer;
import com.entity.Recharge;
import com.file.entity.FileCustomer;
import com.file.service.CustomerService;
import com.interfaces.ChildMeterDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.utils.JsonResult;
import com.utils.Utils;

/**   
* @Title: ValveService.java 
* @Package com.recharge.srevice 
* @author 孙梦楠 
* @date 2017年8月29日 上午9:42:06 
* @version V1.0.0   
*/
@Service
public class ValveService {
	
	
	@Autowired
	private CustomerDao customerdao;
	
	@Autowired
	private CustomerService customerservice;
	
	@Autowired
	private ChildMeterDao childmeterdao;
	
	@Autowired
	private CustomerMeterDao customermeterdao;
	
	//列出所有用户
	@Transactional
	public JsonResult<List<FileCustomer>> listallcustomer(String opeorgcookie, String opetypecookie,String languagecookie,String childtotalstatus ) {

		List<Customer> customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerstatus(
					Integer.valueOf(opeorgcookie), opetypecookie, "2");
		List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();

		if (customers.size() != 0) {
			if (childtotalstatus.equals("总")) {
				for (int i = 0; i < customers.size(); i++) {
					Customer customer = customers.get(i);
					// 和用户关联的表ID
					Integer meterid = customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid();
					// 总分关系
					ChildMeter cm = childmeterdao.findByMeterid(meterid);
					// 如果已设置表而且是总表
					if (cm != null && cm.getFathermeterid() == 0) {
						filecustomers.add(customerservice.dofilecustomer(customer, languagecookie));
					}
				}
			} else {// 分表
				for (int i = 0; i < customers.size(); i++) {
					Customer customer = customers.get(i);
					// 和用户关联的表ID
					Integer meterid = customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid();
					// 总分关系
					ChildMeter cm = childmeterdao.findByMeterid(meterid);
					// 如果是未设置表或者是分表
					if (cm == null// 未设置表
							|| (cm != null && cm.getFathermeterid() == null)// 设置分表但是未关联总表
							|| (cm != null && cm.getFathermeterid() !=0)// 关联过的分表
					) {
						filecustomers.add(customerservice.dofilecustomer(customer, languagecookie));
					}
				}
			}

		}
		return new JsonResult<List<FileCustomer>>(592, "列出所有用户", filecustomers);
		
	}
	


}
